package com.jlboot.core.json;

import com.jlboot.core.consts.CoreResponseCode;

import java.io.Serializable;

/**
 * Created by 51594 on 2022/7/27.
 */
public class JsonResult implements Serializable {


    private Integer code;
    /**
     *
     */
    private String msg;

    /**
     *
     */
    private Object data;

    public static JsonResult ok(){
        return JsonResult.n(CoreResponseCode.SUCCESS.getCode(), CoreResponseCode.SUCCESS.getName(),null);
    }

    public static JsonResult ok(Object data){
        return JsonResult.n(CoreResponseCode.SUCCESS.getCode(), CoreResponseCode.SUCCESS.getName(),data);
    }

    public static JsonResult okWithMsg(String msg){
        return JsonResult.n(CoreResponseCode.SUCCESS.getCode(), msg,null);
    }

    public static JsonResult okWithoutMsg(){
        return JsonResult.n(CoreResponseCode.SUCCESS.getCode(),null,null);
    }

    public static JsonResult okWithoutMsg(Object data){
        return JsonResult.n(CoreResponseCode.SUCCESS.getCode(),null,data);
    }

    public static JsonResult fail(){
        return JsonResult.n(CoreResponseCode.FAIL.getCode(), CoreResponseCode.FAIL.getName(),null);
    }

    public static JsonResult fail(String msg){
        return JsonResult.n(CoreResponseCode.FAIL.getCode(), msg,null);
    }

    public static JsonResult fail(Integer code,String msg){
        return JsonResult.n(code, msg,null);
    }

    public static JsonResult fail(String msg,Object data){
        return JsonResult.n(CoreResponseCode.FAIL.getCode(), msg,data);
    }

    public static JsonResult n(int code, String msg,Object data) {
        JsonResult result = new JsonResult();
        result.setCode(code).setMsg(msg).setData(data);
        return result;
    }

    public int getCode() {
        return code;
    }

    public JsonResult setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public JsonResult setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Object getData() {
        return data;
    }

    public JsonResult setData(Object data) {
        this.data = data;
        return this;
    }

    /**
     * @return
     */
    public String toJson() {
        return JsonUtil.toJsonString(this);
    }
}
